﻿<template>
    <div class="SHZY-common-layout">
        <div class="SHZY-common-layout-center">
            <el-row class="SHZY-common-search-box" :gutter="16">
                <el-form @submit.native.prevent="">
                    <el-col :span="6">
                        <el-form-item label="编号">
                            <el-input v-model="query.code" placeholder="编号" @keyup.enter.native="search" clearable />
                        </el-form-item>
                    </el-col>
                    <el-col :span="6">
                        <el-form-item label="检验批">
                            <el-input v-model="query.inspectionLot" placeholder="检验批" @keyup.enter.native="search"
                                clearable />
                        </el-form-item>
                    </el-col>
                    <el-col :span="6">
                        <el-form-item label="批次">
                            <el-input v-model="query.batchNumber" placeholder="批次" @keyup.enter.native="search" clearable />
                        </el-form-item>
                    </el-col>
                    <template v-if="showAll">
                        <el-col :span="6">
                            <el-form-item label="物料">
                                <el-input v-model="query.material" placeholder="物料" @keyup.enter.native="search"
                                    clearable />
                            </el-form-item>
                        </el-col>
                        <el-col :span="6">
                            <el-form-item label="检验类型">
                                <el-select v-model="query.type" placeholder="检验类型" @keyup.enter.native="search">
                                    <el-option v-for="(item, index) in typeOptions" :key="index" :label="item.fullName"
                                        :value="item.enCode" />
                                </el-select>
                            </el-form-item>
                        </el-col>
                    </template>
                    <el-col :span="6">
                        <el-form-item>
                            <el-button type="primary" icon="el-icon-search" @click="search()">查询</el-button>
                            <el-button icon="el-icon-refresh-right" @click="reset()">重置</el-button>
                            <el-button type="text" icon="el-icon-arrow-down" @click="showAll = true"
                                v-if="!showAll">展开</el-button>
                            <el-button type="text" icon="el-icon-arrow-up" @click="showAll = false" v-else>收起</el-button>
                        </el-form-item>
                    </el-col>
                </el-form>
            </el-row>
            <div class="SHZY-common-layout-main SHZY-flex-main">
                <div class="SHZY-common-head">
                    <div class="SHZY-common-head-right">
                        <el-tooltip effect="dark" content="刷新" placement="top">
                            <el-link icon="icon-ym icon-ym-Refresh SHZY-common-head-icon" :underline="false"
                                @click="reset()" />
                        </el-tooltip>
                    </div>
                </div>
                <SHZY-table v-loading="listLoading" :data="list" @sort-change='sortChange' custom-column>
                    <el-table-column label="编号" prop="code" align="left" sortable='custom' min-width="150">
                    </el-table-column>
                    <el-table-column label="检验批" prop="inspectionLot" align="left" min-width="150">
                    </el-table-column>
                    <el-table-column label="批次" prop="batchNumber" align="left" width="150">
                    </el-table-column>
                    <el-table-column label="物料名称" prop="materialDes" align="left" width="180" :show-overflow-tooltip="true">
                        <template slot-scope="scope">
                            {{ scope.row.materialDes }}
                        </template>
                    </el-table-column>
                    <el-table-column label="物料编码" prop="materialCode" align="left" width="180"
                        :show-overflow-tooltip="true">
                        <template slot-scope="scope">
                            {{ scope.row.materialCode }}
                        </template>
                    </el-table-column>
                    <el-table-column label="检验类型" prop="typeDesc" align="left" width="150">
                    </el-table-column>
                    <el-table-column label="数量" align="left" width="120" prop="type">
                        <template slot-scope="scope">
                            <span v-if="scope.row.type == 'IQC'">{{ scope.row.incomingQty }}</span>
                            <span v-else>{{ scope.row.productionQty }}</span>
                        </template>
                    </el-table-column>
                    <el-table-column label="问题原因" prop="problem" align="left" width="180">
                    </el-table-column>
                    <el-table-column label="使用决策" prop="decisionName" align="left" width="180">
                    </el-table-column>
                    <el-table-column label="创建人" prop="creatorUser" align="left" width="200">
                    </el-table-column>
                    <el-table-column label="创建时间" prop="createTime" align="left" width="120">
                        <template slot-scope="scope">
                            {{ scope.row["createTime"] | toDate("yyyy-MM-dd") }}
                        </template>
                    </el-table-column>
                    <el-table-column prop="status" label="状态" width="120" sortable="custom" fixed="right">
                        <template slot-scope="scope">
                            <el-tag v-if="scope.row.status == 1">等待审核</el-tag>
                            <el-tag type="success" v-else-if="scope.row.status == 2">审核通过</el-tag>
                            <el-tag type="danger" v-else-if="scope.row.status == 3">审核驳回</el-tag>
                            <el-tag type="info" v-else-if="scope.row.status == 4">流程撤回</el-tag>
                            <el-tag type="info" v-else-if="scope.row.status == 5">审核终止</el-tag>
                            <el-tag type="success" v-else-if="scope.row.status == 6">完成</el-tag>
                            <el-tag type="warning" v-else>等待提交</el-tag>
                        </template>
                    </el-table-column>
                    <el-table-column label="操作" width="100" fixed="right">
                        <template slot-scope="scope">
                            <el-button size="mini" type="text" :disabled='[1, 2, 4, 5, 6].indexOf(scope.row.status) > -1'
                                @click="addOrUpdateHandle(scope.row.id, scope.row.type)">编辑
                            </el-button>
                            <el-button size="mini" type="text" :disabled="!scope.row.status"
                                @click="toApprovalDetail(scope.row.id, scope.row.status, scope.row.type)">详情</el-button>
                        </template>
                    </el-table-column>
                </SHZY-table>
                <pagination :total="total" :page.sync="listQuery.currentPage" :limit.sync="listQuery.pageSize"
                    @pagination="initData" />
            </div>
        </div>
        <FlowBox v-if="formVisible" @close="closeForm" ref="Form" />
        <FormBox v-if="formBoxVisible" @close="closeForm" ref="Form" />
    </div>
</template>
<script>
import { getDefecthandleList } from '@/api/q1/business/defecthandle'
import FlowBox from '@/views/workFlow/components/FlowBox'
import FormBox from '@/views/q1/business/defecthandle/form.vue'
export default {
    name: 'defecthandle',
    components: { FlowBox, FormBox },
    data() {
        return {
            showAll: false,
            query: {
                code: undefined,
                inspectionLot: undefined,
                batchNumber: undefined,
                material: undefined,
                type: undefined,
            },
            list: [],
            listLoading: true,
            multipleSelection: [],
            total: 0,
            listQuery: {
                currentPage: 1,
                pageSize: 20,
                sort: "desc",
                sidx: "createTime",
            },
            formBoxVisible: false,
            formVisible: false,
            exportBoxVisible: false,
            typeOptions: [],
        }
    },
    computed: {
        menuId() {
            return this.$route.meta.modelId || ''
        }
    },
    created() {
        this.initData()
        this.getDictionaryData()
    },
    methods: {
        getDictionaryData() {
            this.$store.dispatch('base/getDictionaryData', { sort: 'BusinessType' }).then((res) => {
                this.typeOptions = res
            })
        },
        sortChange({ column, prop, order }) {
            this.listQuery.sort = order == 'ascending' ? 'asc' : 'desc'
            this.listQuery.sidx = !order ? '' : prop
            this.initData()
        },
        initData() {
            this.listLoading = true;
            let _query = {
                ...this.listQuery,
                ...this.query,
                menuId: this.menuId,
            };
            let query = {}
            for (let key in _query) {
                if (Array.isArray(_query[key])) {
                    query[key] = _query[key].join()
                } else {
                    query[key] = _query[key]
                }
            }
            getDefecthandleList(query).then(res => {
                this.list = res.data.list
                this.total = res.data.pagination.total
                this.listLoading = false
            })
        },
        search() {
            this.listQuery = {
                currentPage: 1,
                pageSize: 20,
                sort: "desc",
                sidx: "code",
            }
            this.initData()
        },
        refresh(isrRefresh) {
            this.formVisible = false
            this.formBoxVisible = false
            if (isrRefresh) this.reset()
        },
        reset() {
            for (let key in this.query) {
                this.query[key] = undefined
            }
            this.listQuery = {
                currentPage: 1,
                pageSize: 20,
                sort: "desc",
                sidx: "code",
            }
            this.initData()
        },
        addOrUpdateHandle(id, type) {
            let data = {
                id,
                enCode: 'defecthandle',
                flowId: '457422703645713477',
                formType: 1,
                opType: '-1',
                dataType: type
            }
            // this.formVisible = true
            this.formBoxVisible = true
            this.$nextTick(() => {
                this.$refs.Form.loading = true
                this.$refs.Form.IsOnlyForm = true
                this.$refs.Form.init(data)
            })
        },
        toApprovalDetail(id, status, type) {
            let data = {
                id,
                enCode: 'defecthandle',
                flowId: '457422703645713477',
                formType: 1,
                opType: 0,
                status,
                dataType: type
            }
            if (status == 6) {
                this.formBoxVisible = true
                this.$nextTick(() => {
                    this.$refs.Form.loading = true
                    this.$refs.Form.IsOnlyForm = true
                    this.$refs.Form.init(data)
                })
                return
            }
            this.formVisible = true
            this.$nextTick(() => {
                this.$refs.Form.init(data)
            })
        },
        closeForm(isRefresh) {
            this.formVisible = false
            this.formBoxVisible = false
            if (isRefresh) this.refresh(isRefresh)
        },
    }
}
</script>